﻿//列表搜索请求方法
function Search() {
    $('#PageIndex').val(1);
    return true;
}
//分页提交请求方法
function Paging(Index) {
    $("#PageIndex").val(Index).parents("form").submit();
}
//表单提交前验证方法(带确认提交)
function checkfrom() {
    $("form input:text,form input:password,form textarea").filter("[q],[maxL],[url],[int]").keyup();
    if ($("form input[v]").length > 0) {
        return confirm("您填写数据有误，是否继续保存？");
    } else {
        return true;
    }
}
//表单提交前验证方法(不带确认提交)
function checkfrom2() {
    $("form input:text,form input:password,form textarea").filter("[q],[maxL],[url],[int]").keyup();
    if ($("form input[v]").length > 0) {
        return false;
    } else {
        return true;
    }
}
$(function () {
    //显示验证结果
    function showMsg(obj, result, txt, key) {
        if (result) {
            obj.removeAttr("v");
            obj.parents(".control-group").remove("error").addClass("success");
            obj.next().html(txt ? txt : "<span class='icon-ok'>&nbsp;&nbsp;</span>");
        } else {
            obj.attr("v", key);
            obj.parents(".control-group").removeClass("success").addClass("error");
            obj.next().html(txt);
        }
    }
    //为空判断
    $(".controls input:text[q],.controls input:password[q],.controls textarea[q]").keyup(function () {
        var o = $(this);
        if (!o.attr("v") || o.attr("v") == "q") {
            if ($.trim(o.val()) == "") {
                showMsg(o, false, "此处不能为空！", "q");
            } else {
                showMsg(o, true, "");
            }
        }
    }).blur(function () { $(this).keyup(); });
    //数字判断
    $(".controls input:text[int]").keyup(function () {
        var o = $(this);
        if ($.trim(o.val()) != "" && (!o.attr("v") || o.attr("v") == "int")) {
            if (!(/^\d*$/.test(o.val()))) {
                showMsg(o, false, "只能输入数字！", "q");
            } else {
                showMsg(o, true, "");
            }
        }
    }).blur(function () { $(this).keyup(); });
    //最大字符限制
    $(".controls input:text[maxL],.controls input:password[q],.controls textarea[maxL]").keyup(function () {
        var o = $(this);
        if ($.trim(o.val()) != "" && (!o.attr("v") || o.attr("v") == "maxL")) {
            if (o.val().length > o.attr("maxL")) {
                showMsg(o, false, "输入的字符超过" + o.attr("maxL") + "个！", "maxL");
            } else {
                showMsg(o, true, "");
            }
        }
    }).blur(function () { $(this).keyup(); });
    //URL限制
    $(".controls input:text[url]").keyup(function () {
        var o = $(this);
        if ($.trim(o.val()) != "" && (!o.attr("v") || o.attr("v") == "url")) {
            var reg = /^((http(s)?|ftp|telnet|news|rtsp|mms):\/\/)?(((\w(\-*\w)*\.)+[a-zA-Z]{2,4})|(((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?))(:\d{0,5})?(\/+.*)*$/;
            if (!reg.test(o.val())) {
                showMsg(o, false, "输入的URL不符合规则！", "url");
            } else {
                showMsg(o, true, "");
            }
        }
    }).blur(function () { $(this).keyup(); });
});